<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-09 20:41:17
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-10-24 22:12:49
 -->
<template>
  <div class="app-container">
    <eHeader />
    <!--表格渲染-->
    <div class="flex_box">
      <div class="table_box">
        <el-table v-loading="loading" ref="tableList" :data="tableList" row-key="id" @cell-click="findPdict" height="100%" border fit highlight-current-row>
           <el-table-column prop="dictNo"  label="字典代码" />
          <el-table-column prop="dictName"  label="名称" />
          <el-table-column label="操作" align="center" width="240px">
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="success"
                @click="edit(scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                :loading="delLoading"
                @click="deleteTable1Id(scope.row.id)">删除</el-button>
              <el-button
                size="mini"
                type="primary"
                @click="add(scope.row)">新增</el-button>
            </template>
            
          </el-table-column>
        </el-table>
      </div>
      <div class="table_box ">
        <el-table v-loading="loading" ref="childList" :data="childList" row-key="id" height="100%"  border fit highlight-current-row>
          <el-table-column prop="dictNo"  label="字典代码" />
          <el-table-column prop="dictName"  label="名称" />
          <el-table-column prop="dictVal"  label="值" />
          <el-table-column  label="操作" align="center" >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="success"
                @click="edit(scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                :loading="delLoading"
                @click="deleteTable2Id(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <eForm ref="form" :sup_this="sup_this" :is-add="isAdd" />
    </div>
    
  </div>
</template>

<script>
import eHeader from './module/header'
import eForm from './module/form'
export default {
  components: { eHeader,eForm },
  // mixins: [initData],
  data() {
    return {
      delLoading: false,
      sup_this: this,
      tableList: [],
      childList:[],
      loading: false,
      isAdd:true,
      dictNo:'',
    }
  },
  created() {
    this.getLists('')
  },
  methods: {
    deleteTable1Id(id){
      this.deleteById(id,'table1')
    },
    deleteTable2Id(id){
      this.deleteById(id,'table2')
    },
    deleteById(id,state) {
      this.delLoading = true
      this.$api.fenxiao.sysDict
        .deleteById(id)
        .then(res => {
          this.delLoading = false
          if(state == 'table2'){
            this.findByPdictCode(this.dictNo)
          }
          this.$notify({ title: '删除成功', type: 'success', duration: 2500 })
        })
        .catch(err => {
          this.delLoading = false
          console.log(err.response.data.message)
        })
    },
    getLists(state) {
      this.$api.fenxiao.sysDict.listPDictAll({}).then(res => {
        console.log(res)
        this.tableList = res.data;
        if(state !=''){
          this.findByPdictCode(state)
        }else{
          this.findPdict(res.data[0])
        }
      })
    },
    findPdict(e){
      this.findByPdictCode(e.dictNo)
      this.dictNo = e.dictNo
    },
    findByPdictCode(id){
      console.log(id)
      this.$api.fenxiao.sysDict
        .findByPdictCode(id)
        .then(res => {
          this.childList = res.data
          console.log(this.childList)
        })
        .catch(err => {
          console.log(err.response.data.message)
        })
    },

    edit(row){
      this.isAdd = false;
      const _this = this.$refs.form
      _this.form = { dictName: row.dictName,dictNo: row.dictNo,id:row.id,dictVal:row.dictVal, pdictNo: row.pdictNo }
      //_this.dictNo = row.pdictNo
      _this.dialog = true
    },
    add(row){
      this.isAdd = true;
      const _this = this.$refs.form
      _this.dialog = true;
      _this.dictNo = row.dictNo
      console.log(_this.dictNo)
      _this.form = { dictName: '', pdictNo: row.dictNo}
    }
  }
}
</script>

<style scoped>
.flex_box{
  display: flex;
  /* align-items: center; */
}
.table_box{
  height: 70vh;
  width: 50%;
}
</style>
